﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using BUS;
using DTO;
namespace PTTK11_HuongRung
{
    public partial class frmThemMonAn : Form
    {
        public frmThemMonAn()
        {
            InitializeComponent();
        }

        int row;
        private void btThem_Click(object sender, EventArgs e)
        {
            if (txtTenMonAn.Text.Count()==0)
            {
                MessageBox.Show("Tên Món Ăn không để trống!");
                txtTenMonAn.Focus();
                txtTenMonAn.SelectAll();
                return;
            }
            
            MonAnBUS busma = new MonAnBUS();
            // kiem tra mon an da ton tai chua
            if (busma.KiemTraMonAnTonTai(txtTenMonAn.Text.Trim()) == true)
            {
                MessageBox.Show("Món ăn '" + txtTenMonAn.Text.Trim() + "' đã có trong hệ thống");
                return;
            }

           // MonAnBUS busma = new MonAnBUS();
            busma.ThemMonAn(txtTenMonAn.Text.Trim());

            int ma = busma.GetMonAnID();
            ChiTietMonAnBUS busctma = new ChiTietMonAnBUS();
           
            for (int i = 0; i < dgvDanhSach.Rows.Count; i++)
            {
                ChiTietMonAnDTO dto = new ChiTietMonAnDTO();
                dto.MONANID = ma;
                dto.NVLID = int.Parse(dgvDanhSach.Rows[i].Cells[1].Value.ToString());
                dto.SOLUONGPHANAN = int.Parse(txtSoluong.Text.Trim());
                busctma.ThemMonAn(dto);
            }
            MessageBox.Show("Thêm Thành Công.");
            dgvDanhSach.Rows.Clear();
            txtMaMonAn.Text = "";
            txtTenMonAn.Text = "";
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {
            btVao.Visible = false;
            btDong.Visible = true;
            btSua.Visible = true;
            btXoa.Visible = true;
            btCapNhat.Enabled = false;
            btThem.Enabled = false;
            btThoat.Enabled = false;

        }


        private void btXoa_Click(object sender, EventArgs e)
        {
            dgvDanhSach.Rows[row].Cells.Clear();
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            btCapNhat.Enabled = true;
            btThem.Enabled = true;
            btThoat.Enabled = true;

            btVao.Visible = true;
            btDong.Visible = false;
            btSua.Visible = false;
            btXoa.Visible = false;

        }

        private void btVao_Click(object sender, EventArgs e)
        {
            int i = dgvDanhSach.Rows.Count+1;
            
            for (int j = 0; j < dgvDanhSach.Rows.Count; j++)
                if (cbxTenNVL.Text.CompareTo(dgvDanhSach.Rows[j].Cells[2].Value.ToString()) == 0 && cbxTenNVL.SelectedValue.ToString().CompareTo(dgvDanhSach.Rows[j].Cells[1].Value.ToString()) == 0)
                {
                    MessageBox.Show("Nguyên Vật liệu '" + cbxTenNVL.Text.Trim() + "' đã có trong nguyên liệu.");
                    return;
                }
            dgvDanhSach.Rows.Add(i, cbxTenNVL.SelectedValue, cbxTenNVL.Text, txtSoluong.Text);

        }

        private void frmThemMonAn_Load(object sender, EventArgs e)
        {
            btnDangNhap.Text = "Đăng Xuất";
            NhanVienBUS nvBus = new NhanVienBUS();
            NhanVienDTO nvDto = new NhanVienDTO();
            nvDto = nvBus.GetThongTinNhanVien(Secssion.MaNV);
            txtHoTen.Text = nvDto.TENNHANVIEN.Trim();
            LoaiNhanVienBUS lnvbus = new LoaiNhanVienBUS();
            txtMaNV.Text = lnvbus.GetTenLoaiNV(nvDto.LOAINHANVIEN);
            reLoadNguyenVatLieu();
        }

        private void reLoadNguyenVatLieu()
        {
            NguyenVatLieuBUS bus = new NguyenVatLieuBUS(); cbxTenNVL.DataSource = bus.GetNguyenVatLieu(); cbxTenNVL.DisplayMember = "TEN_NGUYEN_VAT_LIEU"; cbxTenNVL.ValueMember = "NGUYEN_VAT_LIEU_ID";
        }

        private void dgvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            row = e.RowIndex;
            cbxTenNVL.SelectedValue = int.Parse(dgvDanhSach.Rows[e.RowIndex].Cells[1].Value.ToString());
            txtSoluong.Text = dgvDanhSach.Rows[e.RowIndex].Cells[3].Value.ToString();
           // cbxTenNVL.SelectedIndex
        }

        private void btSua_Click(object sender, EventArgs e)
        {
           // dgvDanhSach.Rows.Add(i, cbxTenNVL.SelectedValue, cbxTenNVL.Text, txtSoluong.Text);
            if(row<0)
                return;
            dgvDanhSach.Rows[row].Cells[1].Value = cbxTenNVL.SelectedValue;
            dgvDanhSach.Rows[row].Cells[2].Value = cbxTenNVL.Text;
            dgvDanhSach.Rows[row].Cells[3].Value = txtSoluong.Text;
            
        }

        private void txtSoluong_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmMonAn frm = new frmMonAn();
            frm.ShowDialog();
            this.Close();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text.Trim().CompareTo("Đăng Xuất") == 0)
            {
                btnDangNhap.Text = "Đăng Nhâp";
                Secssion.MaNV = "";
                Secssion.MaKhac = "";

                txtHoTen.Text = "";
                txtMaNV.Text = "";
                this.Hide();
                frmQuanLy frm = new frmQuanLy();
                frm.ShowDialog();
                this.Close();
            }
            else
            {
                this.Hide();
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                this.Close();
            }
        }

      
    }
}
